package br.com.tcc.automacao.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.tcc.automacao.usuario.negocio.Usuario;


/**
 * Servlet Filter implementation class LoginFilter
 */
@WebFilter("/LoginFilter")
public class LoginFilter implements Filter {

    /**
     * Default constructor. 
     */
    public LoginFilter() {
    }


	public void destroy() {
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		Usuario usuario = (Usuario) ((HttpServletRequest) request).getSession().getAttribute("usuarioLogado");
		
//		String url = ((HttpServletRequest)request).getRequestURI();
//		if(url.endsWith("/")){
//			url = url.substring(0, url.length() -1);
//		}
//		
		if(usuario != null){
		    chain.doFilter(request, response);		    
		}else{
		    
		    ((HttpServletResponse) response).sendRedirect
                    (PalavrasProp.ENDERECO+"/login");
		}
	    
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
